import {
  on
} from 'node-notifier'
import {
  config
} from 'shelljs'
import ajax from '../../common/ajax'
import api from '../../common/api'
import encryption from '../../common/encryption'

const state = {
  count: 0,
  haveNum: 0,
  msg: 0,
  saveBlo: true
}


const mutations = {
  changeData(state, obj) {
    state.count = obj
  },
  changeNum(state, obj) {
    state.haveNum = obj
  },
  getUseId(state, obj) {
    state.msg = obj
  },
  setBlo(state, obj) {
    state.saveBlo = obj
  }
}

const actions = {
  saveData(context, userId) {
    ajax.get({
      url: api.customResource.routeName + api.customResource.children.count,
      data: {
        userId: userId
      },
      success(res) {
        console.log(res)
        if (res == '') {
          context.commit('changeData', 0);
        }
        context.commit('changeData', JSON.parse(encryption.decryptionMxr(res)));
        console.log(JSON.parse(encryption.decryptionMxr(res)))
      }
    })
  },
  saveBlo(context, blo) {
    context.commit('setBlo', blo)
  },
  saveHave(context, userId) {
    ajax.get({
      url: api.resource.privateRouteName + api.resource.children.number,
      data: {
        userId: userId
      },
      success(res) {
        context.commit('changeNum', JSON.parse(encryption.decryptionMxr(res)).resourceNum);
        console.log(JSON.parse(encryption.decryptionMxr(res)).resourceNum)
      },
      File(res) {
        console.log(res)
      }
    })
  },
  getUserId(context, userId) {
    ajax.get({
      url: api.Info.routeName + api.Info.children.account + userId,
      success: function (res) {
        context.commit('getUseId', JSON.parse(encryption.decryptionMxr(res)));
      },
    });
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
